//
//  UserManager.m
//  EveryDay
//
//  Created by FengDongsheng on 15/6/25.
//  Copyright (c) 2015年 FengDongsheng. All rights reserved.
//

#import "UserManager.h"
#import "JSLLoginController.h"
#import <objc/runtime.h>
//#import "DSUtils.h"
#import <YYModel/YYModel.h>
//#import "UserOperateRequest.h"

#define USER_MANAGER    @"user_manager"
//#import "MyFundRequest.h"

@implementation UserManager

+ (UserManager*)manager{
    static UserManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[UserManager alloc] init];

    });
    return manager;
}


+ (void)userInfoWithSuccess:(void(^)(UserInfo *userInfo))success
                           failure:(void(^)(NSInteger code,NSString *errorMessage))failure {
    
    [HttpRequest GET:USERINFO_URL parameters:nil success:^(id  _Nullable result, NSString * _Nullable successMessage) {
        
    
        NSLog(@"%@",result);
        NSDictionary *userDict = result;
        
        // 创建用户模型
        UserInfo *userModel =   [UserInfo yy_modelWithJSON:userDict.yy_modelToJSONString];
        // 创建用户模型管理者
        [UserManager manager].userInfo = userModel;
        
        
        if (success) {
            success(userModel);
        }
        
        
    } failure:^(NSInteger code, NSString * _Nullable errorMessage) {
        
        if (failure) {
            failure (code,errorMessage);
        }
        // 清空用户模型
        UserInfo *userModel = [UserInfo yy_modelWithJSON:@""];
        [UserManager manager].userInfo = userModel;
        [UserManager  logout];
        NSLog(@"%@",errorMessage);
  
    }];
}


- (void)encodeWithCoder:(NSCoder *)aCoder{
    unsigned int count = 0;
    Ivar *vars = class_copyIvarList([self class], &count);
    for (int i = 0; i < count; i++) {
        Ivar ivar = vars[i];
        const char *name = ivar_getName(ivar);
        NSString *key = [NSString stringWithUTF8String:name];
        id value = [self valueForKey:key];
        [aCoder encodeObject:value forKey:key];
    }
    free(vars);
}

- (id)initWithCoder:(NSCoder *)aDecoder{
    unsigned int count = 0;
    if (self = [super init]) {
        Ivar *vars = class_copyIvarList([self class], &count);
        for (int i = 0; i < count; i++) {
            Ivar var = vars[i];
            const char *name = ivar_getName(var);
            //归档
            NSString *key = [NSString stringWithUTF8String:name];
            id value = [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
        free(vars);
    }
    return self;
}

+ (BOOL)checkLogin:(UIViewController *)viewController{
    NSString *userToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    if (userToken.length == 0) {
            JSLLoginController *loginVC = [[JSLLoginController alloc] init];
            UINavigationController *vcNavigation = [[UINavigationController alloc] initWithRootViewController:loginVC];
            [[self currentViewController] presentViewController:vcNavigation animated:YES completion:nil];
        
        return NO;
       
    }
    return YES;
}


//获取Window当前显示的ViewController
+ (UIViewController*)currentViewController{
    //获得当前活动窗口的根视图
    UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (1) {
        //根据不同的页面切换方式，逐步取得最上层的viewController
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController*)vc).selectedViewController;
        }
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController*)vc).visibleViewController;
        }
        if (vc.presentedViewController) {
            vc = vc.presentedViewController;
        }else{
            break;
        }
    }
    return vc;
}


+ (BOOL)isLogin{
    NSString *userToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    if (userToken.length > 0) {
        
        return YES;
        
    }
    return NO;
}



- (void)checkSetPhone:(UIViewController *)viewController{
    
}

//退出登录
+ (void)logout{

    
    [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"token"];
    
    [[NSUserDefaults standardUserDefaults] synchronize];
}


@end
